class Post < ActiveRecord::Base
  default_scope :order => 'created_at DESC'
  belongs_to :category, :counter_cache => true
  belongs_to :user
  has_many :comments, :dependent => :destroy
  delegate :name, :to => :category, :prefix => true
  delegate :username, :to => :user, :prefix => true
  cattr_reader :per_page
  @@per_page = 10  # 分页每页10条记录

  # => 增加浏览次数
  def self.increase_view_count(record_id)
    update_counters record_id, :view_count => 1
  end

  # => 当find参数中有:page选项时，就进行分页。
  def self.find(*args)
    options = args.dup.extract_options!
    if options.has_key?(:page)
      self.paginate(*args)
    else
      super
    end
  end

end
